package action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

import dao.AlbumDAO;
import dao.UserDAO;

import bean.Album;
import bean.User;
import bean.UserSnapshot;

public class AlbumAction extends DispatchAction {
	// list all of the album
	public ActionForward browse(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		SimpleDateFormat format =
            new SimpleDateFormat("MMM dd yyyy");
		
		UserDAO userDAO=new UserDAO();
		User user=(User)request.getSession(true).getAttribute("user");
		if(user==null){
			return mapping.findForward("login");			
		}
		user=userDAO.findById(user.getUId());
		Set<Album> albums=(Set<Album>)user.getAlbums();
		StringBuffer responseXML=new StringBuffer();
		responseXML.append("<albums>\n");
		if(!albums.isEmpty()){
			Iterator<Album> i=albums.iterator();
			Album temp=null;
			while(i.hasNext()){
				temp=i.next();
				responseXML.append("<album>\n");
				responseXML.append("<albumId>");
				responseXML.append(temp.getAlbumId());
				responseXML.append("</albumId>\n");
				responseXML.append("<user>");
				responseXML.append(temp.getUser().getUId());
				responseXML.append("</user>\n");
				responseXML.append("<albumName>");
				responseXML.append(temp.getAlbumName());
				responseXML.append("</albumName>\n");
				responseXML.append("<albumLocation>");
				responseXML.append(temp.getAlbumLocation());
				responseXML.append("</albumLocation>\n");
				responseXML.append("<createdAt>");
				responseXML.append(format.format(temp.getCreatedAt()));
				responseXML.append("</createdAt>\n");
				responseXML.append("</album>\n");				
			}
		}		
		responseXML.append("</albums>");
		System.out.println(responseXML.toString());
		response.setContentType("text/xml");
		try {
			ServletOutputStream out=response.getOutputStream();
			out.print(responseXML.toString());			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ActionForward view(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		SimpleDateFormat format =
            new SimpleDateFormat("MMM dd yyyy");
		
		AlbumDAO albumDAO=new AlbumDAO();
		int albumID=Integer.parseInt( request.getParameter("albumid"));
		System.out.println(albumID);
		Album album=albumDAO.findById(albumID);
		
		Set<UserSnapshot> snaps=(Set<UserSnapshot>) album.getUserSnapshots();
		Iterator<UserSnapshot> i=snaps.iterator();
		List<UserSnapshot> snapList=new ArrayList<UserSnapshot>();
		UserSnapshot temp=null;
		StringBuffer responseXML=new StringBuffer();
		
		responseXML.append("<snaps>\n");
		while(i.hasNext()){
			temp=i.next();
			responseXML.append("<snap>\n");
			responseXML.append("<snapId>");
			responseXML.append(temp.getSnapId());
			responseXML.append("</snapId>\n");
			responseXML.append("<albumName>");
			responseXML.append(album.getAlbumName());
			responseXML.append("</albumName>\n");
			responseXML.append("<snapName>");
			responseXML.append(temp.getSnapTitle());
			responseXML.append("</snapName>\n");
			responseXML.append("<snapDate>");
			responseXML.append(format.format( temp.getUploadedAt()) );
			responseXML.append("</snapDate>\n");
			responseXML.append("</snap>\n");	
		}		
		responseXML.append("</snaps>");
		System.out.println(responseXML.toString());
		response.setContentType("text/xml");
		try {
			ServletOutputStream out=response.getOutputStream();
			out.print(responseXML.toString());			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
